from ckeditor.fields import RichTextField
from django.db import models
from common.db import BaseModel
# Create your models here.
class GoodsGroup(models.Model):
    """商品分类表"""
    name=models.CharField(max_length=20)
    image=models.ImageField(max_length=200,blank=True,null=True)
    status=models.BooleanField(default=False,blank=True)
    def __str__(self):
        return self.name
    class Meta:
        db_table = 'GoodsGroup'  # 表名
        verbose_name='商品分类表' #后台显示名称
        verbose_name_plural = verbose_name

class Goods(BaseModel):
    """商品表"""
    group=models.ForeignKey('GoodsGroup',max_length=20,on_delete=models.CASCADE)
    title=models.CharField(max_length=200,default='')
    desc=models.CharField(max_length=200)
    price=models.DecimalField(max_digits=10,decimal_places=2)
    cover=models.ImageField(max_length=200,verbose_name='封面图链接',blank=True,null=True)
    stock=models.IntegerField(default=1,verbose_name='库存',blank=True)
    sales=models.IntegerField(default=0,verbose_name='销量',blank=True)
    is_on=models.BooleanField(default=False,verbose_name='是否上架',blank=True)
    recommend=models.BooleanField(default=False,verbose_name='是否推荐',blank=True)

    def __str__(self):
        return self.title
    class Meta:
        db_table = 'Goods'  # 表名
        verbose_name='商品表' #后台显示名称
        verbose_name_plural = verbose_name

class Detail(BaseModel):
    """商品详情"""
    goods=models.OneToOneField('Goods',on_delete=models.CASCADE)#一对一，级联删除
    producer=models.CharField(max_length=200)
    norms=models.CharField(max_length=200)
    details=RichTextField(blank=True)

    class Meta:
        db_table = 'Detail'  # 表名
        verbose_name = '商品详情'  # 后台显示名称
        verbose_name_plural = verbose_name


class GoodsBanner(BaseModel):
    """商品轮播图"""
    title=models.CharField(max_length=20,default='')
    image=models.ImageField(max_length=200,blank=True,null=True)
    status=models.BooleanField(default=False,blank=True,verbose_name='是否启用')
    seq=models.IntegerField(verbose_name='顺序',default=1,blank=True)

    def __str__(self):
        return self.title

    class Meta:
        db_table = 'GoodsBanner'  # 表名
        verbose_name = '商品轮播图'  # 后台显示名称
        verbose_name_plural = verbose_name

class Collect(models.Model):
    """收藏商品"""
    user=models.ForeignKey('user.User',on_delete=models.CASCADE,blank=True)
    goods=models.ForeignKey('Goods',on_delete=models.CASCADE)

    def __str__(self):
        return self.goods

    class Meta:
        db_table = 'Collect'  # 表名
        verbose_name = '商品收藏'  # 后台显示名称
        verbose_name_plural = verbose_name
